package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IEmployeeService employeeService;
    @RequestMapping("/list")
    @RequiresPermissions(value = {"employee:list","员工列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        PageInfo result = employeeService.query(qo);
        model.addAttribute("result",result);
        //给部门下拉框查询时提供数据
        List<Department> depts = departmentService.list();
        model.addAttribute("depts",depts);
        return "/employee/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"employee:list","员工删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        employeeService.delete(id);
        return new JsonResult();
    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"employee:input","员工编辑页面"},logical = Logical.OR)
    public String input(Long id,Model model){
        //提供部门的数据给添加和编辑用
        List<Department> depts = departmentService.list();
        model.addAttribute("depts", depts);
        //给角色选择框提供数据
        List<Role> roles = roleService.list();
        model.addAttribute("roles",roles);
        if(id != null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        return "/employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"employee:saveOrUpdate","员工添加/编辑"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Employee employee,Long[] ids){
        if(employee.getId() != null){
            employeeService.update(employee,ids);
        }else {
            employeeService.save(employee,ids);
        }
        return new JsonResult();
    }


    //批量删除员工
    @RequestMapping("/batchDelete")
    @ResponseBody
    @RequiresPermissions(value = {"employee:batchDelete","员工批量删除"},logical = Logical.OR)
    public JsonResult batchDelete(Long[] ids){
        employeeService.deleteBatch(ids);
        return new JsonResult();
    }

    //检查用户名是否重复
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Boolean> checkName(String name){
        Employee employee = employeeService.selectByName(name);
        Map<String,Boolean> map = new HashMap<>();
        map.put("valid",employee == null);
        return map;
    }

    //导出
    @RequestMapping("exportXls")
    public void export(HttpServletResponse response) throws Exception {
        //设置文件下载的响应头
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //创建excel文件对象
        Workbook workbook = employeeService.exportXls();
        workbook.write(response.getOutputStream());
    }

    //导入
    @RequestMapping("importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws Exception {
        System.out.println(file.getOriginalFilename());
        employeeService.importXls(file);
        return new JsonResult();
    }

}
